﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Common;



namespace Sequence
{
    public partial class FrmQueryDef : Form
    {
        public DbConnection m_connection;
        public string sID;
        private string sSql;

        private DbConnection GetConntion()
        {
            return m_connection;
        }

        public FrmQueryDef()
        {
            InitializeComponent();
        }

        public FrmQueryDef(string sType)
        {
            InitializeComponent();
            switch (sType)
            {
                case "AlisWebService":
                case "QueryDef":
                case "QueryDefUpdate":
                    this.Text = "QueryDef";
                    sSql = Sequence.CSqlString.GetQuerydef("OCI", textBox1.Text.ToLower());
                    break;
                case "QueryDefProcess":
                    this.Text = "QueryDef Process";
                    sSql = Sequence.CSqlString.GetQuerydefProcess("OCI", textBox1.Text.ToLower());
                    break;

            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (sSql != null)
            {
                try
                {
                    DbCommand command = GetConntion().CreateCommand();
                    command.CommandText = sSql.Replace("%%", "%" + textBox1.Text + "%").ToLower();
                    command.CommandType = CommandType.Text;
                    DbDataReader oDataReader = command.ExecuteReader();

                    lvQueries.Items.Clear();
                    while (oDataReader.Read())
                    {
                        ListViewItem oItem = lvQueries.Items.Add(oDataReader[0].ToString());
                        oItem.SubItems.Add(oDataReader[1].ToString());
                    }
                    oDataReader.Close();
                    oDataReader.Dispose();
                    command.Dispose();

                }
                catch (Exception ex)
                {
                    DialogResult dialogResult = MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void FrmQueryDef_Load(object sender, EventArgs e)
        {

        }

        private void cmdOpen_Click(object sender, EventArgs e)
        {
            cmdOpenClick();            
        }

        private void cmdOpenClick()
        {
            if (lvQueries.SelectedItems.Count > 0)
            {
                ListView.SelectedListViewItemCollection items = lvQueries.SelectedItems;
                ListViewItem lvItem = items[0];
                sID = lvItem.SubItems[1].Text;
            }
            this.Close();
        }

        private void lvQueries_DoubleClick(object sender, EventArgs e)
        {
            cmdOpenClick();            
        }

        private void cmdCancel_Click(object sender, EventArgs e)
        {
            sID = "";
            this.Close();
        }
    }
}
